package com.zy.common.utils;

import com.alibaba.fastjson2.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Map;


/**
 * 调用API接口判断日期是否是工作日 周末还是节假日
 * 工作日对应结果为 1, 节假日对应的结果为 2
 */
public class HolidayUtil {

    private final static String APPKEY = "52984";
    private final static String SIGN = "db6c3c970d4164a306a3f03ed66a80d9";

    public static int request(String httpArg) {
        String httpUrl = "http://api.k780.com/?app=life.workday&appkey="+APPKEY+"&sign="+SIGN+"&format=json";
        String result = null;
        httpUrl = httpUrl + "&date=" + httpArg;

        int d=0;//工作日对应结果为 1, 节假日对应的结果为 2

        try {
            URL url = new URL(httpUrl);
            InputStream in=url.openStream();
            ByteArrayOutputStream out=new ByteArrayOutputStream();
            try {
                byte buf[]=new byte[1024];
                int read = 0;
                while ((read = in.read(buf)) > 0) {
                    out.write(buf, 0, read);
                }
            }  finally {
                if (in != null) {
                    in.close();
                }
            }
            byte b[]=out.toByteArray( );
            result = new String(b,"utf-8");
            JSONObject ob= JSONObject.parseObject(result);
            if(ob!=null){
                Map<String, String> map = (Map<String, String>)ob.get("result");
                d= Integer.parseInt(map.get("workmk"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d;
    }


}
